<?php

namespace eu\freeplace\page;

/**
 * Dynamic Loader for JavaScript Files.
 *
 * @author Elmar Dott
 * @version 1.0
 */
class JSLoader {

    /** Class Name */
    const CLASS_NAME = "JSLoader";

    /**
     * Default Constructor
     * @global Logger $logger
     */
    public function __construct() {
        global $logger;
        $logger->log("Calling instance of ".self::CLASS_NAME."()", "NOTICE");
    }

    /**
     * Render the complete JS Inclusions of the Webapp.
     *
     * @global Logger $logger
     * @return String fullJSinclusion
     */
    public function render() {
        global $logger;
        $logger->log(self::CLASS_NAME."().loadFiles()", "DEBUG");
        return trim($this->loadFiles());
    }

    // fetch all JS Files in the JS Folder
    private function loadFiles() {

        global $utils;
        $js = "";
        $files = $utils->readDir('javascript/');
        $cnt   = sizeof($files);
        if($cnt > 0) {
            for($i=0; $i < $cnt; $i++) {
                $js .= "<script src='js/".$files[$i]."' type='text/javascript'></script>\n";
            }
        }
        return $js;
    }

}//CLASS

?>